module('test',package.seeall)
local memcached = require('memcached')
local slt2 = require('slt2')
local user = {
    name = 'world'
}

function index() 
	local tmpl = slt2.loadstring([[<span>
	#{ if user ~= nil then }#
	Hello, #{= user.name }#!
	#{ else }#
	<a href="/login">login</a>
	#{ end }#
	</span>
	]])	
	return "test" .. request.host .. global.config.db.host .. slt2.render(tmpl, {user = user})
end

function memcache() 
    local memc, err = memcached:new()
    if not memc then
        return "failed to instantiate memc: " .. err
    end

    memc:set_timeout(1000) -- 1 sec

    -- or connect to a unix domain socket file listened
    -- by a memcached server:
    --     local ok, err = memc:connect("unix:/path/to/memc.sock")

    local ok, err = memc:connect("192.168.3.104", 11211)
    if not ok then
        return "failed to connect: " .. err
    end

    local ok, err = memc:flush_all()
    if not ok then
       return "failed to flush all: " .. err
    end

    local ok, err = memc:set("dog", 32)
    if not ok then
        return "failed to set dog: " .. err
    end

    local res, flags, err = memc:get("dog")
    if err then
        return "failed to get dog: " .. err
    end

    if not res then
        return "dog not found"
    end

    return "dog: " .. res

    -- put it into the connection pool of size 100,
    -- with 10 seconds max idle timeout
    --local ok, err = memc:set_keepalive(10000, 100)
    --if not ok then
    --    return "cannot set keepalive: " .. err
    --end

    -- or just close the connection right away:
    -- local ok, err = memc:close()
    -- if not ok then
    --     ngx.say("failed to close: ", err)
    --     return
    -- end
end
